package org.binglan.youziyuepai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.binglan.youziyuepai.entity.UserToPhotographyTag;
import org.binglan.youziyuepai.mapper.UserToPhotographyTagMapper;
import org.binglan.youziyuepai.service.UserToPhotographyTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户与摄影标签多对多表 服务实现类
 * </p>
 *
 * @author sanjin
 * @since 2019-04-09
 */
@Service
public class UserToPhotographyTagServiceImpl extends ServiceImpl<UserToPhotographyTagMapper, UserToPhotographyTag> implements UserToPhotographyTagService {

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean isExists(String userId, Integer photographyTagId) {
        UserToPhotographyTag userToPhotographyTag = new UserToPhotographyTag();
        userToPhotographyTag.setUserId(userId);
        userToPhotographyTag.setPhotographyTagId(photographyTagId);
        return getOne(new QueryWrapper<>(userToPhotographyTag)) != null;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean deleteAllByUserId(String userId) {
        UserToPhotographyTag userToPhotographyTag = new UserToPhotographyTag();
        userToPhotographyTag.setUserId(userId);
        return this.remove(new QueryWrapper<>(userToPhotographyTag));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean saveUserToPhotographyTagByBatch(List<UserToPhotographyTag> userToPhotographyTagList) {
        if (userToPhotographyTagList.size() == 0) return true;

        List<UserToPhotographyTag> collect = userToPhotographyTagList.stream().map(item -> {
            item.setId(Sid.nextShort());
            return item;

        }).collect(Collectors.toList());
        return this.saveBatch(collect);
    }
}
